<?php
namespace Ctrl;

use Lib;
use Model\JobModel;
class Job extends Lib\Base\Ctrl
{
    private $db;
    private $version;
    private $season;
    private $where;
    private $jobModel;
    public function __construct()
    {
        //校验管理员权限
        $roleService = new Lib\Paas\Role();
        $checkAdmin = $roleService->getRoleInfo();
//        if ($checkAdmin['role'] != ROLE_ID) {
//            die('抱歉！您没有访问权限');
//        }
//        if (!in_array(MODULE_ID, $checkAdmin['moduleID']) ) {
//            die('抱歉！您没有访问权限');
//        }
        $this->db = Lib\Db\DbModel::init();

        //获取当前版本
        $this->version = $this->db->table('tbversion')->where(['isCurrent'=>1])->find('id');
        $this->season = $this->db->table('tbseason')->where(['isCurrent'=>1])->find('id');
        /*if (empty($this->version) || empty($this->season)){
            header("Location: ". U('main/main'));
        }*/
        $this->where = [
            'versionId' =>$this->version['id'],
            'seasonId' =>$this->season['id'],
            'isDelete'=>0
        ];
        $this->assign('iActId','1001');
        $this->jobModel = new JobModel();
    }

    //种族视图首页
    public function index()
    {
        $keywords = I('post.keywords');
        if ($keywords && !empty($keywords)) {
            $this->where['name'] = ['like','%'.$keywords.'%'];
        }

        $jobs = $this->jobModel->getAllJob($this->where);

        if (I('post.')) {
            $this->ajaxSuccess("成功",$jobs);
        }

        /*if( empty($jobs)) {
            header("Location: ". U('main/main'));
        }*/


        $this->assign('jobs',$jobs);

        $this->display('Admin/Job/index');
    }
    //获取单个种族的信息
    public function getJobInfo()
    {
        $jobId = I('jobId');
        $job = $this->jobModel->getJobInfo($jobId,$this->where);

        if(!empty($job)) {
            $job['level'] =  json_decode($job['level']);
            $this->ajaxSuccess("成功",$job);

        } else{
            $this->ajaxError('获取失败');
        }
    }

    //修改种族信息
    public function update()
    {

        $jobId = I('jobId');
        $name = I('name');
        $traitId = I('traitId');
        $alias = I('alias');
        $introduce = I('introduce');
        $level_key = I('level_key');
        $level_name = I('level_name');

        $TFTID = I('TFTID');
        if (!is_numeric($TFTID)) {
            $this->ajaxError('TFTID必须为数字类型');
        }

        if (!is_numeric($jobId)) {
            $this->ajaxError('职业ID必须为数字类型');
        }
        if (empty($name)) {
            $this->ajaxError('职业名称不能为空');
        }
        if (!is_numeric($traitId)) {
            $this->ajaxError('职业效果ID必须为数字类型');
        }
        if (empty($alias)) {
            $this->ajaxError('职业英文名称不能为空');
        }
        if (empty($introduce)) {
            $this->ajaxError('职业效果简介称不能为空');
        }
        //检查当前赛季和版本TFTID是否重复
        if($this->jobModel->checkTFTID($jobId,$this->where,$TFTID)){
            $this->ajaxError('该TFTID已存在');
        }
        $level = [];
        foreach ($level_key as $key){
            $description = $level_name[$key-1];

            //验证
            if(!$description) {

                $res = [
                    'type' =>0,
                    'status' => 'error',
                    'msg' => "技能描述(等级".$key.")不能为空",
                ];
                $this->ajaxReturn($res, 'json');

                break;
            }
            $level[$key] = $level_name[$key-1];
        }

        $user = new Lib\Paas\Login();
        $userName = $user->getLoginInfo()['name'];
        $data = [
            'TFTID'=>$TFTID,
            'name'=>$name,
            'traitId'=>$traitId,
            'introduce'=>$introduce,
            'alias'=>$alias,
            'level'=> json_encode($level),
            'updateMan'=>$userName,
            'updateTime'=>date('Y-m-d H:i:s'),
        ];
        $res = $this->jobModel->updateJob($jobId,$this->where,$data);

        logWrite(parent::LOG_TYPE_EDIT,$res,'tbjob');
        if($res!==false){
            $this->ajaxSuccess("编辑成功");
        } else {

            $this->ajaxError('编辑失败');
        }
    }

    public function add()
    {
        $this->display('Admin/Job/add');
    }

    public function addSave()
    {
        $jobId = I('jobId');
        $name = I('name');
        $traitId = I('traitId');
        $alias = I('alias');
        $introduce = I('introduce');
        $level_key = I('level_key');
        $level_name = I('level_name');
        $TFTID = I('TFTID');
        if (!is_numeric($TFTID)) {
            $this->ajaxError('TFTID必须为数字类型');
        }
        if (!is_numeric($jobId)) {
            $this->ajaxError('职业ID必须为数字类型');
        }
        if (empty($name)) {
            $this->ajaxError('职业名称不能为空');
        }
        if (!is_numeric($traitId)) {
            $this->ajaxError('职业效果ID必须为数字类型');
        }
        if (empty($alias)) {
            $this->ajaxError('职业英文名称不能为空');
        }
        if (empty($introduce)) {
            $this->ajaxError('职业效果简介称不能为空');
        }

        //检查当前赛季和版本TFTID是否重复
        if($this->jobModel->checkTFTID($jobId,$this->where,$TFTID)){
            $this->ajaxError('该TFTID已存在');
        }

        $level = [];
        foreach ($level_key as $key){
            $description = $level_name[$key-1];
            //验证
            if(!$description) {
                $res = [
                    'type' =>0,
                    'status' => 'error',
                    'msg' => "技能描述(等级".$key.")不能为空",
                ];
                $this->ajaxReturn($res, 'json');
                break;
            }
            $level[$key] = $level_name[$key-1];
        }

        $user = new Lib\Paas\Login();
        $userName = $user->getLoginInfo()['name'];
        $data = [
            'TFTID'=>$TFTID,
            'jobId'=>$jobId,
            'name'=>$name,
            'traitId'=>$traitId,
            'introduce'=>$introduce,
            'alias'=>$alias,
            'level'=> $level,
            'creater'=>$userName,
            'createTime'=>date('Y-m-d H:i:s'),
        ];

        $where = [
            'versionId' => $this->version['id'],
            'seasonId' => $this->season['id'],
            'isDelete' => 1
        ];
        if ($this->jobModel->getJobInfo($jobId,$where)) {
            $this->ajaxError('该职业已存在');
        }
        $res = $this->jobModel->insertJobOne($this->version['id'],$this->season['id'],$data);
        logWrite(parent::LOG_TYPE_INSENT,$res,'tbjob');
        if($res!==false){

            $this->ajaxSuccess("添加成功");
        } else {

            $this->ajaxError('添加失败');
        }
    }
}